package com.work.lxykeeper.base;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.work.lxykeeper.R;
import com.work.lxykeeper.module.main.MainActivity;
import com.work.lxykeeper.utils.ViewUtils;

import java.lang.reflect.Field;


/**
 * @author Created by WZ on 2015年12月18日
 * @category 基类Fragment
 */
public abstract class BaseFragment extends Fragment implements OnClickListener {
    protected View mView;
    protected BaseActivity mActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (BaseActivity) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mView != null) {
            ViewUtils.removeSelfFromParent(mView);
        }
        mView = loadView(inflater,container,savedInstanceState);
        initView();
        return mView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        initData();
        super.onViewCreated(view, savedInstanceState);
    }


    /**
     * 返回界面布局
     */
    protected abstract View getLayoutView(LayoutInflater inflater, ViewGroup container,
                                          Bundle savedInstanceState);

    /**
     * 初始化布局
     */
    protected abstract void initView();

    /**
     * 初始化界面数据
     */
    protected void initData() {
    }

    /**
     * 处理点击事件
     */
    protected void processClick(View v) {

    }

    /**
     * Fragment专用findView
     */
    public View findViewById(int id) {
        return mView.findViewById(id);
    }

    /**
     * 加载布局
     */
    public View loadView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        if (getLayoutView(inflater,container,savedInstanceState) != null) {
            return getLayoutView(inflater,container,savedInstanceState);
        } else {
            return View.inflate(getActivity(), R.layout.page_default, null);
        }
    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        //如果是主页的几个模块碎片就做此处理
        if (getView() != null && getActivity().getClass() == MainActivity.class) {
            getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
        }
        super.setMenuVisibility(menuVisible);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                getActivity().finish();
                break;
            default:
                processClick(v);
                break;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class
                    .getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


}
